package cn.hua.douban.room.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author hua
 * @date 2021年05月13日 11:41
 */
public class HttpUtils {
    /**
     * @param
     *
     * @author hua
     * @date 2021/5/13 11:43 
     * @return org.jsoup.nodes.Document
     */
    public static Document requestProxy(String url, String cookie){

        StringBuilder html = null;
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置请求参数
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("Cookie", cookie);
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
            connection.setReadTimeout(15000);

            // 发送POST请求必须设置如下两行
            connection.setDoInput(true);
            connection.setDoOutput(true);

            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            html = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                html.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Jsoup.parse(String.valueOf(html));
    }
}
